package bsf.baseservice.businessmq.producter.loadbalance;

import bsf.baseservice.businessmq.model.tb_mqpath_partition_model;
import bsf.baseservice.businessmq.producter.LoadBalanceNodeInfo;
import bsf.baseservice.businessmq.systemruntime.PartitionIDInfo;
import bsf.baseservice.businessmq.systemruntime.PartitionRuleHelper;
import bsf.baseservice.businessmq.systemruntime.log.ErrorLogHelper;

import java.util.concurrent.atomic.AtomicInteger;

public class SystemLoadBalance extends BaseLoadBalance
{
	protected AtomicInteger sendMessageCount = new AtomicInteger(0);
	@Override
	public LoadBalancePartitionInfo getLoadBalancePartitionInfo()
	{
	      int sendmessagecount =sendMessageCount.getAndIncrement();
		if (sendmessagecount > 1000000000)
		{
			sendMessageCount.set(1);
		}

		if (this.getMQPathParitionModels().size() > 0)
		{
			LoadBalanceNodeInfo info = new LoadBalanceNodeInfo();
			int index = (sendmessagecount - 1) % getMQPathParitionModels().size();
			tb_mqpath_partition_model partion = getMQPathParitionModels().get(index);
			PartitionIDInfo partitionidinfo = PartitionRuleHelper.getPartitionIDInfo(partion.partitionid);
			LoadBalancePartitionInfo t = new LoadBalancePartitionInfo();
			t.PartitionId=partion.partitionid;
			t.PartitionIndex=partion.partitionindex;
			t.MQPathParitionModel=partion;
			return t;
		}
		else
		{
			ErrorLogHelper.writeLine(-1, "", "SystemLoadBalance-LoadBalancePartitionInfo", "系统默认生产者负载均衡出错:当前可用分区数为0", new RuntimeException());
			return null;
		}
	}
}